<template>
  <div class="app-container">
    <el-table  v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品唯一标识" align="center" prop="productMarking" />
      <el-table-column label="所属品类ID" align="center" prop="productClassifiedId" />
      <el-table-column label="所属品类名称" align="center" prop="productClassifiedName" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="所属项目" align="center" prop="projectId" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="所属部门" align="center" prop="deptId" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="产品状态" align="center" prop="productState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_status" :value="scope.row.productState"/>
        </template>
      </el-table-column>
      <el-table-column label="物模型定义" align="center" prop="metadata" />
      <el-table-column label="设备图标" align="center" prop="photoUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photoUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="连网方式" align="center" prop="networkWay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.network_way" :value="scope.row.networkWay"/>
        </template>
      </el-table-column>
      <el-table-column label="消息协议ID" align="center" prop="messageProtocol" />
      <el-table-column label="消息协议名称" align="center" prop="protocolName" />
      <el-table-column label="传输协议" align="center" prop="transportProtocol" />
      <el-table-column label="数据存储策略相关配置" align="center" prop="storePolicyConfiguration" />
      <el-table-column label="设备接入方式名称" align="center" prop="accessName" />
      <el-table-column label="设备接入方式" align="center" prop="accessProvider" />
      <el-table-column label="数据存储策略" align="center" prop="storePolicy" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="预留字段1" align="center" prop="attr1" />
      <el-table-column label="预留字段2" align="center" prop="attr2" />
      <el-table-column label="预留字段3" align="center" prop="attr3" />
      <el-table-column label="预留字段4" align="center" prop="attr4" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dev:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dev:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品唯一标识" prop="productMarking">
          <el-input v-model="form.productMarking" placeholder="请输入产品唯一标识" />
        </el-form-item>
        <el-form-item label="所属品类ID" prop="productClassifiedId">
          <el-input v-model="form.productClassifiedId" placeholder="请输入所属品类ID" />
        </el-form-item>
        <el-form-item label="所属品类名称" prop="productClassifiedName">
          <el-input v-model="form.productClassifiedName" placeholder="请输入所属品类名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属项目" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入所属项目" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属部门" />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="产品状态" prop="productState">
          <el-select v-model="form.productState" placeholder="请选择产品状态">
            <el-option
              v-for="dict in dict.type.device_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物模型定义" prop="metadata">
          <el-input v-model="form.metadata" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备图标" prop="photoUrl">
          <image-upload v-model="form.photoUrl"/>
        </el-form-item>
        <el-form-item label="连网方式" prop="networkWay">
          <file-upload v-model="form.networkWay"/>
        </el-form-item>
        <el-form-item label="消息协议ID" prop="messageProtocol">
          <el-input v-model="form.messageProtocol" placeholder="请输入消息协议ID" />
        </el-form-item>
        <el-form-item label="消息协议名称" prop="protocolName">
          <el-input v-model="form.protocolName" placeholder="请输入消息协议名称" />
        </el-form-item>
        <el-form-item label="传输协议" prop="transportProtocol">
          <el-input v-model="form.transportProtocol" placeholder="请输入传输协议" />
        </el-form-item>
        <el-form-item label="数据存储策略相关配置" prop="storePolicyConfiguration">
          <el-input v-model="form.storePolicyConfiguration" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备接入方式名称" prop="accessName">
          <el-input v-model="form.accessName" placeholder="请输入设备接入方式名称" />
        </el-form-item>
        <el-form-item label="设备接入方式" prop="accessProvider">
          <el-input v-model="form.accessProvider" placeholder="请输入设备接入方式" />
        </el-form-item>
        <el-form-item label="数据存储策略" prop="storePolicy">
          <el-input v-model="form.storePolicy" placeholder="请输入数据存储策略" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="attr1">
          <el-input v-model="form.attr1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="attr2">
          <el-input v-model="form.attr2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="attr3">
          <el-input v-model="form.attr3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="attr4">
          <el-input v-model="form.attr4" placeholder="请输入预留字段4" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/dev/product";

export default {
  name: "ProductAdd",
  dicts: ['device_status', 'device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productMarking: null,
        productClassifiedId: null,
        productClassifiedName: null,
        deviceType: null,
        projectId: null,
        projectName: null,
        deptId: null,
        deptName: null,
        productState: null,
        metadata: null,
        photoUrl: null,
        networkWay: null,
        messageProtocol: null,
        protocolName: null,
        transportProtocol: null,
        storePolicyConfiguration: null,
        accessName: null,
        accessProvider: null,
        storePolicy: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        productMarking: [
          { required: true, message: "产品唯一标识不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标记不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productMarking: null,
        productClassifiedId: null,
        productClassifiedName: null,
        deviceType: null,
        projectId: null,
        projectName: null,
        deptId: null,
        deptName: null,
        productState: null,
        metadata: null,
        photoUrl: null,
        networkWay: null,
        messageProtocol: null,
        protocolName: null,
        transportProtocol: null,
        storePolicyConfiguration: null,
        accessName: null,
        accessProvider: null,
        storePolicy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品管理编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dev/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
